
#include <iostream>
#include <boost/thread.hpp>
#include <boost/asio.hpp> 
#include <string>
#include <boost/array.hpp> 
 
 void handler0(const boost::system::error_code &ec) 
 { 
   std::cout << "5 s." << std::endl; 
    sleep(10);
 } 
 
 int main0() 
 { 
   boost::asio::io_service io_service;  //一个 I/O 服务 io_service，用于初始化 I/O 对象 timer。 
   std::cout << "0 s." << std::endl; 
   boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(5)); //该闹钟在 timer 被定义之后立即开始计时
   std::cout << "1 s." << std::endl; 
   timer.async_wait(handler0); //非阻塞式的。 I/O 对象通常还提供了阻塞式的方法，可以让执行流在特定操作完成之前保持阻塞。
   std::cout << "2 s." << std::endl; 
   io_service.run(); //因为控制权必须被操作系统接管，才能在五秒之后调用 handler() 函数
   std::cout << "3 s." << std::endl; 
 }



 void handler1(const boost::system::error_code &ec) 
 { 
   std::cout << "5 s." << std::endl; 
 } 
 
 void handler2(const boost::system::error_code &ec) 
 { 
   std::cout << "10 s." << std::endl; 
 } 
 
 int main1() 
 { 
   boost::asio::io_service io_service; 
   boost::asio::deadline_timer timer1(io_service, boost::posix_time::seconds(5)); 
   timer1.async_wait(handler1); 
   boost::asio::deadline_timer timer2(io_service, boost::posix_time::seconds(10)); 
   timer2.async_wait(handler2); 
   std::cout << "1 s." << std::endl; 
   io_service.run(); 
   std::cout << "2 s." << std::endl; 
 }

 boost::asio::io_service io_service1; 
 boost::asio::io_service io_service2; 
 
 void run1() 
 { 
   io_service1.run(); 
 } 
 
 void run2() 
 { 
   io_service2.run(); 
 } 
 
 int main2() 
 { 
   boost::asio::deadline_timer timer1(io_service1, boost::posix_time::seconds(5)); 
   timer1.async_wait(handler1); 
   boost::asio::deadline_timer timer2(io_service2, boost::posix_time::seconds(5)); 
   timer2.async_wait(handler2); 
   boost::thread thread1(run1); 
   boost::thread thread2(run2); 
   thread1.join(); 
   thread2.join(); 
 }

 boost::asio::io_service io_service;
 boost::asio::ip::tcp::resolver resolver(io_service);
 boost::asio::ip::tcp::socket sock(io_service);
 boost::array<char, 4096> buffer;

//  connect_handler() 和 read_handler() 函数会分别在连接被建立后以及接收到数据后被调用
 void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred)
 {
     if (!ec)
     {
         std::cout << "------------------------------------------" << std::endl;
         std::cout << std::string(buffer.data(), bytes_transferred) << std::endl;
         sock.async_read_some(boost::asio::buffer(buffer), read_handler);
         std::cout << "------------------------------------------" << std::endl;
     }
 }

 void connect_handler(const boost::system::error_code &ec)
 {
     if (!ec)
     {
         boost::asio::write(sock, boost::asio::buffer("GET / HTTP 1.1\r\nHost: highscore.de\r\n\r\n"));
         sock.async_read_some(boost::asio::buffer(buffer), read_handler);
     }
 }
// 互联网使用了所谓的IP地址来标识每台PC。 IP地址实际上只是一长串数字，难以记住。 而记住象 www.highscore.de 这样的名字就容易得多。
//  为了在互联网上使用类似的名字，需要通过一个叫作域名解析的过程将它们翻译成相应的IP地址。 这个过程由所谓的域名解析器来完成，对应的 I/O 对象是：boost::asio::ip::tcp::resolver
 void resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it)
 {
     if (!ec)
     {
         sock.async_connect(*it, connect_handler);
     }
 }

 int main3()
 {
     boost::asio::ip::tcp::resolver::query query("www.highscore.de", "80");
     resolver.async_resolve(query, resolve_handler);
     io_service.run();
 }


 boost::asio::io_service io_service_server; 
 boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 80); 
 boost::asio::ip::tcp::acceptor acceptor(io_service_server, endpoint); 
 boost::asio::ip::tcp::socket sock1(io_service_server); 
 std::string data = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!"; 
 
 void write_handler(const boost::system::error_code &ec, std::size_t bytes_transferred) 
 {
    std::cout << "------------------------------------------" << std::endl;
 } 
 
 void accept_handler(const boost::system::error_code &ec)
 { 
   if (!ec) 
   { 
     boost::asio::async_write(sock1, boost::asio::buffer(data), write_handler); 
   } 
 } 
 
 int main() 
 { 
   acceptor.listen(); 
   acceptor.async_accept(sock1, accept_handler); 
   io_service_server.run(); 
 }